package kau.dao;

import java.util.ArrayList;
import java.util.HashMap;

import kau.common.DBUtil;
import kau.exception.DuplicationException;
import kau.exception.RecordNotFoundException;
import kau.vo.Customer;
import kau.vo.Reply;

import org.apache.ibatis.session.SqlSession;

public class ReplyDAOImpl implements ReplyDAOIF {

	DBUtil db = null;
	private static ReplyDAOImpl instance;
	
	private ReplyDAOImpl(){
		db = DBUtil.getInstance();
	}
	
	public static ReplyDAOImpl getInstance(){
		if(instance == null) instance = new ReplyDAOImpl();
		return instance;
	}
	
	private boolean isExist(int rNumber){
		SqlSession session = db.getSession();
		boolean flag = false;
		String result = null;
		try{
			result = session.selectOne("dbConfig.ReplyMapper.isExist", rNumber);
		}finally{
			session.close();
		}
		if(result!= null) flag= true;
		
		return flag;
		
	}
	@Override
	public void insert(Reply reply)
			throws DuplicationException {
		
		if(isExist(reply.getrNumber())) throw new DuplicationException(reply);
		SqlSession session = db.getSession();
		try{
			session.insert("dbConfig.ReplyMapper.insert", reply);
			session.commit();
		}finally{
			session.close();
		}
	}
	


	@Override
	public void update(int rNumber, String content)
			throws RecordNotFoundException {
		
		HashMap info = new HashMap();
		info.put("rNumber", rNumber);
		info.put("content", content);
		//info.put("writeTime", writeTime);
		
		if(!isExist(rNumber)) throw new RecordNotFoundException(rNumber);
		SqlSession session = db.getSession();
		try{
			session.update("dbConfig.ReplyMapper.update", info);
			session.commit();
		}finally{
			session.close();
		}
	}

	
	@Override
	public void delete(int rNumber)
			throws RecordNotFoundException {
		if(!isExist(rNumber)) throw new RecordNotFoundException(rNumber);
		SqlSession session = db.getSession();
		try{
			session.delete("dbConfig.ReplyMapper.delete", rNumber);
			session.commit();
		}finally{
			session.close();
		}
	}


	@Override
	public Reply getReply(int rNumber) throws RecordNotFoundException {
		if(!isExist(rNumber)) throw new RecordNotFoundException(rNumber);
		SqlSession session = db.getSession();
		Reply reply_ret = null;
		try{
			reply_ret = session.selectOne("dbConfig.ReplyMapper.getReply", rNumber);
			session.commit();
		}finally{
			session.close();
		}
		return reply_ret;
	}

	

	@Override
	public ArrayList<Reply> getAllReplies(int wNumber) {
		ArrayList<Reply> replyList = new ArrayList<Reply>();
		SqlSession session = db.getSession();
		try{
			replyList =(ArrayList) session.selectList("dbConfig.ReplyMapper.getAllReplies",wNumber);
		} finally{
			if(session!=null) session.close();
		}
		return replyList;
	}

	@Override
	public int getMaxrNumber() {
		int number = 1;
		SqlSession session = db.getSession();
		try {
			Integer temp=session.selectOne("dbConfig.ReplyMapper.getMaxrNumber");
			if(temp!=null)number=temp+1;
		} finally {
			if (session != null)
				session.close();
		}
		return number;
	}

	@Override
	public int getReplyCount(int wNumber) {
		// TODO Auto-generated method stub
		int number = 0;
		SqlSession session = db.getSession();
		try {
			Integer temp=session.selectOne("dbConfig.ReplyMapper.getReplyCount",wNumber);
			if(temp!=null)number=temp;
		} finally {
			if (session != null)
				session.close();
		}
		return number;
	}

}
